﻿using System.Collections;

namespace Crux.Core.SQL.Formatter.Core.Util;
/// <summary>
/// 
/// </summary>
/// <typeparam name="T"></typeparam>
public class JSLikeList<T> : IEnumerable
{
    private readonly List<T> tList;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="tList"></param>
    public JSLikeList(List<T> tList)
    {
        this.tList = tList ?? new List<T>();
    }
    /// <summary>
    /// 
    /// </summary>
    public List<T> ToList()
    {
        return tList;
    }

    /// <summary>
    /// 
    /// </summary>
    public JSLikeList<R> Map<R>(Func<T, R> mapper)
    {
        return new JSLikeList<R>(
            tList
            .Select(mapper.Invoke)
            .ToList());
    }

    /// <summary>
    /// 
    /// </summary>
    public string Join(string delimiter)
    {
        return string.Join(delimiter, tList);
    }

    /// <summary>
    /// 
    /// </summary>
    public JSLikeList<T> With(List<T> other)
    {
        return new JSLikeList<T>(
            tList
            .Concat(other)
            .ToList());
    }

    /// <summary>
    /// 
    /// </summary>
    public string Join()
    {
        return Join(",");
    }
    /// <summary>
    /// 
    /// </summary>
    public bool IsEmpty()
    {
        return tList is null || tList.Count == 0;
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public T? Get(int index)
    {
        if (index < 0 || tList.Count <= index)
        {
            return default;
        }

        return tList.ElementAt(index);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public IEnumerator GetEnumerator()
    {
        return tList.GetEnumerator();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public int Size()
    {
        return tList.Count;
    }
}